﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace SpellChecker
{
  public class SpellChecker
  {
    public static bool HasSpellingErrors(string text)
    {
      text = CleanText(text);

      var cleanedText = CleanText(text);
      var wordsToCheck = SplitIntoWords(cleanedText);

      var knownWords = Load("Test");

      return true;
    }

    internal static string CleanText(string text)
    {
      return text;
    }

    internal static IEnumerable<string> SplitIntoWords(string text)
    {
      return text.Split(new char[] { '.', '?', '\'', '\"', '!', ';', '\t', '\r', '\n', ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
    }

    internal static bool CheckWords(IEnumerable<string> wordsToCheck, Dictionary<string, bool> knownWords)
    {
      var allWordsKnown = true;
      foreach (var wordToCheck in wordsToCheck)
      {
        allWordsKnown &= knownWords.ContainsKey(wordToCheck);
      }

      return allWordsKnown;
    }

    internal static Dictionary<string, bool> Load(string filename)
    {
      var dictionary = new Dictionary<string, bool>();
      foreach (var line in File.ReadAllLines(filename))
      {
        dictionary.Add(line.ToUpper(), true);

      }

      return dictionary;
    }

    internal static Dictionary<string, bool> ExpandDictionary(Dictionary<string, bool> dictionary)
    {
      var newDictionary = new Dictionary<string, bool>();
      foreach (var wordEntry in dictionary.Keys)
      {
        var words = wordEntry.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);

        if (words.Length == 0)
          continue;

        foreach (var word in words)
        {
          if (word.Contains("~"))
          {
            word.Replace("~", words[0]);
          }

          newDictionary.Add(word, true);

        }
      }

      return newDictionary;
    }
  
  
  }
}
